const done = true;
const p1 = new Promise((resolve, reject) => {
  setTimeout(() => {
    if (done) {
      resolve("111");
    } else {
      reject("222");
    }
  }, 2000);
});
const p2 = new Promise((resolve, reject) => {
  if (done) {
    resolve("333");
  } else {
    reject("444");
  }
});

const proall = (arr) => {
  if (!Array.isArray(arr)) {
    throw new Error("请传入数组");
  }
  return new Promise((resolve, reject) => {
    let newArr = [];
    // 表示成功的个数
    let successCount = 0;
    arr.forEach((item, index) => {
      item
        .then((res) => {
          newArr[index] = res;
          // 长度相同，说明每一项都成功了
          // if (arr.length === newArr.length) {
          // resolve(newArr);
          // console.log(newArr.every((item) => item));
          // }
          successCount++;
          if (successCount === arr.length) {
            resolve(newArr);
          }
        })
        .catch((err) => {
          reject(err);
        });
    });
  });
};

proall([p1, p2])
  .then((res) => {
    console.log(res);
  })
  .catch((err) => {
    console.log(err);
  });
